﻿/// <reference path="../jquery-1.8.0.js" />
/// <reference path="../jquery-ui-1.8.23.js" />
/// <reference path="../modernizr-2.5.3.js" />
/// <reference path="../jquery-dialogHelper.js" />
/// <reference path="../jquery-sewenEditor.js" />

$(function () {
    // Read current user name
    var currentUserName = $("#menu .username span").text();
    var pageId = $("body").attr("id");

    // Perform common tasks
    InitUI();          // Initialize UI widgets
    ShowHeaderImage(); // Show dynamic banner image
});

function InitUI() {
    // Extended entry
    if (!Modernizr.inputtypes.date) {
        $.datepicker.regional["cs"] = {
            closeText: "Zavřít",
            prevText: "&#x3c;Dříve",
            nextText: "Později&#x3e;",
            currentText: "Dnes",
            monthNames: ["leden", "únor", "březen", "duben", "květen", "červen", "červenec", "srpen", "září", "říjen", "listopad", "prosinec"],
            monthNamesShort: ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII"],
            dayNames: ["neděle", "pondělí", "úterý", "středa", "čtvrtek", "pátek", "sobota"],
            dayNamesShort: ["ne", "po", "út", "st", "čt", "pá", "so"],
            dayNamesMin: ["ne", "po", "út", "st", "čt", "pá", "so"],
            weekHeader: "Týd",
            dateFormat: "yy-mm-dd",
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ""
        };
        $.datepicker.setDefaults($.datepicker.regional["cs"]);
        $("input[type=date]").datepicker();
    }
    $("textarea[data-exteditortype=wikimarkup]").sewenEditor({
        culture: "cs",
        textboxClass: "textbox",
        toolbarButtons: "B:Tučné;I:Kurzíva;U:Podtržené;S:Přeškrtnuté;Sup:Horní index;Sub:Dolní index;;H1:Nadpis 1;H2:Nadpis 2;H3:Nadpis 3;H4:Nadpis 4;H5:Nadpis 5;H6:Nadpis 6;;WikiLink:Odkaz na stránku wiki;ExtLink:Externí odkaz (URL);;Ul:Seznam s odrážkami;Ol:Číslovaný seznam;;Image:Obrázek;CodeBlock:Zdrojový kód;;Preview:Náhled",
        customCommand: function (textarea, command) {
            switch (command) {
                case "Preview":
                    var jqxhr = $.post("/wiki/preview", { markupHtml: textarea.val() }, HandlePreviewResponse, "html");
                    jqxhr.error(function (jqXHR, textStatus, errorThrown) { window.alert("Async HTTP request failed!\r\n" + textStatus + ": " + errorThrown); });
                    break;
                default:
                    window.alert("Invalid command:" + command);
            }
        }
    });

    // Dialog helper
    $().dialogHelper({
        confirmTitle: "FluffyCloud",
        confirmYes: "Ano",
        confirmNo: "Ne",
    });

    // Tabs
    var selectedTabKey = "LastKnownTabIndex[" + window.location.pathname + "]";
    var pagetabs = $(".tabs").tabs({
        select: function (event, ui) {
            // Navigate to URL-based tabs
            var a = $(".tabs ul li a")[ui.index];
            if (a.attributes["data-follow-link"]) {
                var url = $.data(ui.tab, 'load.tabs');
                if (url) {
                    location.href = url.replace("?$", "#");
                    return false;
                }
            } else {
                // Remember last known selected tab in session storage
                if (Modernizr.sessionstorage) window.sessionStorage[selectedTabKey] = ui.index;
            }
            return true;
        }
    });
    if (pagetabs && Modernizr.sessionstorage && (window.location.hash == null || window.location.hash == "")) {
        var selectedTabIndex = parseInt(window.sessionStorage[selectedTabKey], 10);
        if (selectedTabIndex != null) pagetabs.tabs("select", selectedTabIndex);
    }
    // Set default tab
    if ($(".deftab")) {
        $(".tabs").tabs("select", $(".deftab").attr("href"));
    }
    // Switch tabs from link
    $(".tabselect").click(function () {
        $(".tabs").tabs("select", $(this).attr("href"));
    });
}

function ShowHeaderImage() {
    $.getJSON("/Content/HeaderImages/list.json", function (data) {
        var imageIndex = Math.floor(Math.random() * data.length);
        var banner = $("#banner");
        banner.css("background-image", "url(" + data[imageIndex].url + ")");
        banner.attr("title", "Autor obrázku: " + data[imageIndex].author);
    });
}

function HandlePreviewResponse(html) {
    var dialog = $(html);
    $("a[href]", dialog).click(function () { return false; });
    dialog.dialog({
        title: "Preview",
        modal: true,
        show: "clip",
        hide: "clip",
        minWidth: 400,
        minHeight: 400,
        width: Math.min($(window).width() - 200, 1000),
        height: $(window).height() - 200,
        buttons: {
            OK: function () {
                $(this).dialog("close");
            }
        }
    });
}